package com.ssyt.yec.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import com.ssyt.yec.common.Constant;

public class DateUtil {

	public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

	private DateUtil() {

	}

	/**
	 * Date -> String
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date) {
		return format(date, DEFAULT_PATTERN);
	}

	public static String format(Date date, String pattern) {
		if (date == null || StringUtils.isBlank(pattern)) {
			return "";
		}
		return new SimpleDateFormat(pattern).format(date);
	}

	/**
	 * String -> Date
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String dateStr) throws ParseException {
		return parse(dateStr, DEFAULT_PATTERN);
	}

	/**
	 * Date -> String
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String dateStr, String pattern) throws ParseException {

		Validate.notBlank(dateStr, "dateStr" + Constant.VALIDATE_NOT_BLANK);
		Validate.notBlank(pattern, "date pattern" + Constant.VALIDATE_NOT_BLANK);
		return new SimpleDateFormat(pattern).parse(dateStr);
	}

}
